package com.tiantian.quanminxiu.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.presenter.AuthenticationPresent;

import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.ActivityBase;
import com.tiantian.quanminxiu.utils.AddressPickTask;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.AuthenticationView;

import java.util.Locale;

import cn.addapp.pickers.common.LineConfig;
import cn.addapp.pickers.entity.City;
import cn.addapp.pickers.entity.County;
import cn.addapp.pickers.entity.Province;
import cn.addapp.pickers.listeners.OnItemPickListener;
import cn.addapp.pickers.picker.SinglePicker;
import rx.functions.Action1;

/**
 * Created by admin on 2016/9/12.
 */
public class Activity_Authentication extends ActivityBase implements AuthenticationView
{

    public static final int REQUESTE_CODE_IDCARD_FRONT = 0x1001;
    public static final int REQUESTE_CODE_IDCARD_BACK = 0x1002;
    public static final int REQUESTE_CODE_ANCHOR_COVER = 0x1003;

    private String url = "http://www.quanminxiu.tv/agreement/user";
    private AuthenticationPresent present;

    private View vAnthentication1,vAnthentication2,vAnthentication3;
    private View vSubmit1,vSubmit2,vSubmit3,txtAnthenticationWeb;
    private EditText etAnchorName,etAnchorIDCard,etAnchorBankCode,etAnchorBankName;
    private TextView etAnchorBankType,etAnchorBankCity;
    private EditText etAnchorRealName,etAnchorPhone,etAnchorQQ,etAnchorMail;
    private ImageView imgIDCardFront,imgIDCardBack,imgAnchorCover;
    private RadioButton rbSubmit1,rbAnchorWoman,rbAnchorMan;
    private RadioGroup rgAnchorSex;

    private boolean isUploadIDCardFront,isUploadIDCardBack,isUploadAnchorCover;
    private int step =1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_authentication);

        Init();
        InitView();

        present = new AuthenticationPresent();
        present.attachView(this);

        UserData.Reload.Activity_Authentication = true;
    }

    @Override
    public void InitView() {
        vAnthentication1 =findViewById(R.id.vAnthentication1);
        vAnthentication2 =findViewById(R.id.vAnthentication2);
        vAnthentication3 =findViewById(R.id.vAnthentication3);
        vSubmit1 =findViewById(R.id.vSubmit1);
        vSubmit2 =findViewById(R.id.vSubmit2);
        vSubmit3 =findViewById(R.id.vSubmit3);
        txtAnthenticationWeb =findViewById(R.id.txtAnthenticationWeb);

        etAnchorName = (EditText) findViewById(R.id.etAnchorName);
        etAnchorIDCard = (EditText) findViewById(R.id.etAnchorIDCard);
        etAnchorBankCode = (EditText) findViewById(R.id.etAnchorBankCode);
        etAnchorBankType = (TextView) findViewById(R.id.etAnchorBankType);
        etAnchorBankCity = (TextView) findViewById(R.id.etAnchorBankCity);
        etAnchorBankName = (EditText) findViewById(R.id.etAnchorBankName);

        etAnchorRealName = (EditText) findViewById(R.id.etAnchorRealName);
        etAnchorPhone = (EditText) findViewById(R.id.etAnchorPhone);
        etAnchorQQ = (EditText) findViewById(R.id.etAnchorQQ);
        etAnchorMail = (EditText) findViewById(R.id.etAnchorMail);

        imgIDCardFront = (ImageView) findViewById(R.id.imgIDCardFront);
        imgIDCardBack = (ImageView) findViewById(R.id.imgIDCardBack);
        imgAnchorCover = (ImageView) findViewById(R.id.imgAnchorCover);

        rbSubmit1 = (RadioButton) findViewById(R.id.rbSubmit1);
        rbAnchorWoman = (RadioButton) findViewById(R.id.rbAnchorWoman);
        rbAnchorMan = (RadioButton) findViewById(R.id.rbAnchorMan);

        rgAnchorSex = (RadioGroup) findViewById(R.id.rgAnchorSex);

        RxView.clicks(vSubmit1).map(View->vSubmit1).subscribe(OC_Button);
        RxView.clicks(vSubmit2).map(View->vSubmit2).subscribe(OC_Button);
        RxView.clicks(vSubmit3).map(View->vSubmit3).subscribe(OC_Button);

        RxView.clicks(imgIDCardFront).map(View->imgIDCardFront).subscribe(OC_Button);
        RxView.clicks(imgIDCardBack).map(View->imgIDCardBack).subscribe(OC_Button);
        RxView.clicks(imgAnchorCover).map(View->imgAnchorCover).subscribe(OC_Button);

        RxView.clicks(etAnchorBankType).map(View->etAnchorBankType).subscribe(OC_Button);
        RxView.clicks(etAnchorBankCity).map(View->etAnchorBankCity).subscribe(OC_Button);

        RxView.clicks(mHeader.txtPreviousStep).map(View->mHeader.txtPreviousStep).subscribe(OC_Button);
        RxView.clicks(txtAnthenticationWeb).map(View->txtAnthenticationWeb).subscribe(OC_Button);

    }

    private Action1<View> OC_Button = v->{
        switch (v.getId()){
            case R.id.txtPreviousStep:
                switch (step){
                    case 1:
                        break;
                    case 2:
                        vAnthentication1.setVisibility(View.VISIBLE);
                        vAnthentication2.setVisibility(View.GONE);
                        vAnthentication3.setVisibility(View.GONE);
                        step = 1;
                        break;
                    case 3:
                        vAnthentication1.setVisibility(View.GONE);
                        vAnthentication2.setVisibility(View.VISIBLE);
                        vAnthentication3.setVisibility(View.GONE);
                        step = 2;
                        break;
                }
                break;
            case R.id.txtAnthenticationWeb:
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(url);
                intent.setData(content_url);
                thisContext.startActivity(intent);
                Loading_Close();
                break;
            case R.id.vSubmit1:
                if (checkFirstStep()) {
                    vAnthentication1.setVisibility(View.GONE);
                    vAnthentication2.setVisibility(View.VISIBLE);
                    vAnthentication3.setVisibility(View.GONE);
                    step = 2;
                }
                break;
            case R.id.vSubmit2:
                if (checkSecondStep()) {
                    vAnthentication1.setVisibility(View.GONE);
                    vAnthentication2.setVisibility(View.GONE);
                    vAnthentication3.setVisibility(View.VISIBLE);
                    step = 3;
                }
                break;
            case R.id.vSubmit3:
                if (checkThridStep()) {//提交
                    String[] address = etAnchorBankCity.getText().toString().split("-");
                    String Account_Province = "";
                    String Account_City = "";
                    try{
                        Account_Province = address[0];
                        Account_City = address[1];
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                    int Sex = rgAnchorSex.getCheckedRadioButtonId()==R.id.rbAnchorMan?1:2;//性别 1:男 2:女
                    String RealName = etAnchorRealName.getText().toString();
                    String Phone = etAnchorPhone.getText().toString();
                    String Account_RealName = etAnchorName.getText().toString();//开户人姓名
                    String Account_CardNo = etAnchorBankCode.getText().toString();//开户帐号
                    String Account_BankFullName = etAnchorBankName.getText().toString();//开户银行全称

                    String Account_BankName = etAnchorBankType.getText().toString();//开户银行
                    String QQ = etAnchorQQ.getText().toString();
                    String Email = etAnchorMail.getText().toString();
                    String IDCard = etAnchorIDCard.getText().toString();
                    present.applyenter(Sex,RealName,Phone,Account_RealName,Account_CardNo,Account_BankFullName,Account_Province,Account_City,
                            Account_BankName, QQ,Email,IDCard);
                }
                break;
            case R.id.etAnchorBankCity:
                selectRegion();
                break;
            case R.id.etAnchorBankType:
                selectBank();
                break;
            case R.id.imgIDCardFront:
                APPUtils.UploadPictur_To(thisActivity,REQUESTE_CODE_IDCARD_FRONT,false);
                break;
            case R.id.imgIDCardBack:
                APPUtils.UploadPictur_To(thisActivity,REQUESTE_CODE_IDCARD_BACK,false);
                break;
            case R.id.imgAnchorCover:
                APPUtils.UploadPictur_To(thisActivity,REQUESTE_CODE_ANCHOR_COVER,false);
                break;
        }
    };

    @Override
    public void PageInit() {
        if(!UserData.Reload.Activity_Authentication){
            return;
        }
        UserData.Reload.Activity_Authentication = false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        PageInit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        present.detachView();
    }

    public boolean checkFirstStep(){
        String msg = "";
        if(TextUtils.isEmpty(etAnchorRealName.getText())){
            msg = "请填写手机真实姓名";
        }else if(TextUtils.isEmpty(etAnchorPhone.getText())){
            msg = "请填写手机";
        }else if(!APPUtils.checkMobileNumber(etAnchorPhone.getText().toString())){
            msg = "请填写正确手机";
        }else if(TextUtils.isEmpty(etAnchorQQ.getText())){
            msg = "请填写QQ";
        }else if(TextUtils.isEmpty(etAnchorMail.getText())){
            msg = "请填写邮箱";
        }else if(!APPUtils.checkEmail(etAnchorMail.getText().toString())){
            msg = "请填写正确邮箱";
        }
        if(TextUtils.isEmpty(msg)){
            return true;
        }else {
            CommonUtils.showToask(thisContext,msg);
            return false;
        }
    }

    public boolean checkSecondStep(){
        String msg = "";
        if(TextUtils.isEmpty(etAnchorName.getText())){
            msg = "请填写申请人姓名";
        }else if(TextUtils.isEmpty(etAnchorIDCard.getText())){
            msg = "请填写身份证号码";
        }else if(TextUtils.isEmpty(etAnchorBankCode.getText())){
            msg = "请填写银行名称银行卡号";
        }else if(TextUtils.isEmpty(etAnchorBankType.getText())){
            msg = "请选择银行类型";
        }else if(TextUtils.isEmpty(etAnchorBankCity.getText())){
            msg = "请选择银行地区";
        }else if(TextUtils.isEmpty(etAnchorBankName.getText())){
            msg = "请填写银行名称";
        }else if(!isUploadIDCardFront){
            msg = "请上传主播身份证正面";
        }else if(!isUploadIDCardBack){
            msg = "请上传主播身份证反面";
        }else if(!rbSubmit1.isChecked()){
            msg = "请选择同意条款";
        } else if (etAnchorBankCity.getText().toString().split("-").length!=2){
            msg = "银行开户地址错误,请重试";
        }

        if(TextUtils.isEmpty(msg)){
            return true;
        }else {
            CommonUtils.showToask(thisContext,msg);
            return false;
        }
    }

    public boolean checkThridStep(){
        String msg = "";
        if(!isUploadAnchorCover){
            msg = "请上传主播封面";
        }

        if(TextUtils.isEmpty(msg)){
            return true;
        }else {
            CommonUtils.showToask(thisContext,msg);
            return false;
        }
    }

    @Override
    public void uploadAnchorPicFront(boolean isFront)
    {
        CommonUtils.showToask(thisContext,isFront?"上传身份证正面成功":"上传身份反面成功");
        if (isFront){
            isUploadIDCardFront = true;
        }else {
            isUploadIDCardBack = true;
        }
    }

    @Override
    public void uploadAnchorPicCover()
    {
        CommonUtils.showToask(thisContext,"上传主播封面成功");
        isUploadAnchorCover = true;
    }

    @Override
    public void uploadAnchorInfo(boolean success,String msg)
    {
        if (success){
            CommonUtils.showToask(thisContext,"小主的申请已提交，请小主耐心等待审核结果!");
        }else {
            CommonUtils.showToask(thisContext,msg);
        }
        finish();
    }

    @Override
    public void selectBank()
    {
        SinglePicker<String> picker = new SinglePicker<>(this,
                 new String[]{
                        "中国银行", "中国工商银行", "中国建设银行", "中国农业银行", "中国邮政储蓄银行", "交通银行",
                        "招商银行", "广大银行", "广发银行", "民生银行", "浦发银行", "中信银行",
                        "华夏银行", "兴业银行", "北京银行", "平安银行"
                });
        picker.setCanLoop(false);//不禁用循环
        picker.setTopBackgroundColor(0xFFEEEEEE);
        picker.setTopHeight(50);
        picker.setTopLineColor(0xFF33B5E5);
        picker.setTopLineHeight(1);
        picker.setTitleText("请选择银行");
        picker.setTitleTextColor(0xFF999999);
        picker.setTitleTextSize(12);
        picker.setCancelTextColor(0xFF33B5E5);
        picker.setCancelTextSize(13);
        picker.setSubmitTextColor(0xFF33B5E5);
        picker.setSubmitTextSize(13);
        picker.setSelectedTextColor(0xFFEE0000);
        picker.setUnSelectedTextColor(0xFF999999);
        LineConfig config = new LineConfig();
        config.setColor(0xFFEE0000);//线颜色
        config.setAlpha(140);//线透明度
        config.setRatio((float) (1.0 / 8.0));//线比率
        picker.setLineConfig(config);
        picker.setItemWidth(200);
        picker.setBackgroundColor(0xFFE1E1E1);
        //picker.setSelectedItem(isChinese ? "处女座" : "Virgo");
        picker.setSelectedIndex(7);
        picker.setOnItemPickListener(new OnItemPickListener<String>() {
            @Override
            public void onItemPicked(int index, String item) {
                etAnchorBankType.setText(item);
            }
        });
        picker.show();
    }

    @Override
    public void selectRegion()
    {
        AddressPickTask task = new AddressPickTask(this);
        task.setHideCounty(true);
        task.setCallback(new AddressPickTask.Callback() {
            @Override
            public void onAddressInitFailed() {
                CommonUtils.showToask(thisContext,"数据初始化失败");
            }

            @Override
            public void onAddressPicked(Province province, City city, County county) {
                etAnchorBankCity.setText(province.getAreaName()+"-"+city.getAreaName());
            }
        });
        task.execute();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode== RESULT_OK)
        {
            switch (requestCode){
                case REQUESTE_CODE_IDCARD_FRONT:
                    if (data!=null&&!TextUtils.isEmpty(data.getStringExtra("path"))){
                        APPUtils.displayImage("file://"+data.getStringExtra("path"),imgIDCardFront);
                        present.useranchorIDCard(data.getStringExtra("path"),true);
                    }
                    break;
                case REQUESTE_CODE_IDCARD_BACK:
                    if (data!=null&&!TextUtils.isEmpty(data.getStringExtra("path"))){
                        APPUtils.displayImage("file://"+data.getStringExtra("path"),imgIDCardBack);
                        present.useranchorIDCard(data.getStringExtra("path"),false);
                    }
                    break;
                case REQUESTE_CODE_ANCHOR_COVER:
                    if (data!=null&&!TextUtils.isEmpty(data.getStringExtra("path"))){
                        APPUtils.displayImage("file://"+data.getStringExtra("path"),imgAnchorCover);
                        present.anchorCover(data.getStringExtra("path"));
                    }
                    break;
            }
        }
    }
}
